#include<reg51.h>
#define uchar unsigned uchar 
#define uint unsigned int

void delay(uint ms)
{
 uint i,j;
 for(i=0;i<=ms;i++)
    for(j=0;j<=115;j++);
} 

int anjian()
{
 uint t,key=0;
 P3=0xfe;
 t=P3;
 t=t&0xf0;
 if(t!=0xf0)
   {
   	 delay(3);
	 t=P3;
     t=t&0xf0;
	    if(t!=0xf0)
		   {
		    t=P3;
			switch(t)
			  {
			    case 0xee:key=1;break;
		    	case 0xde:key=2;break;
				case 0xbe:key=3;break;
				case 0x7e:key=4;break;
		      }
		   }
   }
   P3=0xfd;
   t=P3;
   t=t&0xf0;
 if(t!=0xf0)
   {
   	 delay(3);
	 t=P3;
     t=t&0xf0;
	    if(t!=0xf0)
		   {
		    t=P3;
			switch(t)
			  {
			    case 0xed:key=5;break;
		    	case 0xdd:key=6;break;
				case 0xbd:key=7;break;
				case 0x7d:key=8;break;
		      }
		   }
   }
  P3=0xfb;
  t=P3;
  t=t&0xf0;
 if(t!=0xf0)
   {
   	 delay(3);
	 t=P3;
     t=t&0xf0;
	    if(t!=0xf0)
		   {
		    t=P3;
			switch(t)
			  {
			    case 0xeb:key=9;break;
		    	case 0xdb:key=10;break;
				case 0xbb:key=11;break;
				case 0x7b:key=12;break;
		      }
		   }
   }
 P3=0xf7;
 t=P3;
 t=t&0xf0;
 if(t!=0xf0)
   {
   	 delay(3);
	 t=P3;
     t=t&0xf0;
	    if(t!=0xf0)
		   {
		    t=P3;
			switch(t)
			  {
			    case 0xe7:key=13;break;
		    	case 0xd7:key=14;break;
				case 0xb7:key=15;break;
				case 0x77:key=16;break;
		      }
		   }
   }
 return key;
}

void main()
{
 while(1)
   {
    switch(anjian())
	  {
	   case 1:P1=0xfe; delay(500);P1=0xff;break;
       case 2:P1=0xfd;delay(500);P1=0xff;break;
	   case 3:P1=0xfb;delay(500);P1=0xff;break;
	   case 4:P1=0xf7;delay(500);P1=0xff;break;
	   case 5:P1=0xef;delay(500);P1=0xff;break;
       case 6:P1=0xdf;delay(500);P1=0xff;break;
	   case 7:P1=0xbf;delay(500);P1=0xff;break;
	   case 8:P1=0x7f;delay(500);P1=0xff;break;
	   case 9:P1=0xfe;delay(500);P1=0xff;break;
	   case 10:P1=0xfd;delay(500);P1=0xff;break;
	   case 11:P1=0xfb;delay(500);P1=0xff;break;
	   case 12:P1=0xf7;delay(500);P1=0xff;break;
	   case 13:P1=0xef;delay(500);P1=0xff;break;
	   case 14:P1=0xdf;delay(500);P1=0xff;break;
	   case 15:P1=0xbf;delay(500);P1=0xff;break;
	   case 16:P1=0x7f;delay(500);P1=0xff;break;	  
	   
	  } 
   
   }
}


































